/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import DAO.Constantes;
import DAO.DAO_Revista;
import DAO.DAO_Seccion;
import actionForm.ModificarRevistaActionForm;
import clases.Revista;
import clases.Seccion;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Rafa
 */
public class AnadirRevistaAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String CANCEL = "cancel";
    private static final String ANADIR = "anadir";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (isCancelled(request)) {
            return mapping.findForward(CANCEL);
        } else {

            ModificarRevistaActionForm formulario = (ModificarRevistaActionForm) form;
            DAO_Seccion daoSeccion = new DAO_Seccion();
            if (formulario.getUpdate() != null) {
                String ret = SUCCESS;
                if (formulario.getUpdate().equals("Anadir")) {
                    List<Seccion> listaSecciones = daoSeccion.consultaSecciones(true);
                    
                    formulario.setNumSeccionesTotales(listaSecciones.size());
                    formulario.setListaTodasSecciones(listaSecciones);
                    request.setAttribute("listaTodasSecciones", listaSecciones);
                    ret = ANADIR;
                }
                return mapping.findForward(ret);
            } else {
                /*Se pone tambien en request la lista de usuarios libres para que esten 
                 disponibles al añadir una nueva seccion o modificar una seccion*/

                if (formulario.getFechaPublicacion().isEmpty()
                        || formulario.getSeccionesEnRevista().length == 0) {

                    formulario.setError(Constantes.getERROR_FORM());
                    return mapping.findForward(FAILURE);

                } else {

                    DAO_Revista daoRevista = new DAO_Revista();

                    String[] secciones = formulario.getSeccionesEnRevista();
                    String fechaPublicacion = formulario.getFechaPublicacion();

                    Revista revista = daoRevista.insertaRevista(secciones, fechaPublicacion, true);

                    if (revista.getIdRevista() == null) {
                        formulario.setErrorAlert(Constantes.getERROR_FORM_ADD());
                    }

                    return mapping.findForward(SUCCESS);
                }
            }
        }
    }
}
